package org.wangle.seckill;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.ObjectUtils;
import org.wangle.api.goods.pojo.Goods;
import org.wangle.api.goods.provider.GoodsProvider;
import org.wangle.api.order.provider.OrdersProvider;
import org.wangle.api.properties.MQ.SecKillProperties;
import org.wangle.seckill.mq.component.delay.SecKillDelayBinding;
import org.wangle.seckill.mq.component.delay.SecKillDelayQueue;
import org.wangle.seckill.utils.DoLua;
import org.wangle.seckill.utils.RedisUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SecKillApplication {

    public static void main(String[] args) {
        SpringApplication.run(SecKillApplication.class, args);
    }

    @Reference
    private GoodsProvider goodsService;

    @Reference
    private OrdersProvider ordersService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void initLua() throws Exception{
        ResourceScriptSource resourceScriptSource2 = new ResourceScriptSource(new ClassPathResource("script/goosNums.lua"));
        DoLua.goodsScript = resourceScriptSource2.getScriptAsString();
        RedisUtils redisUtils = new RedisUtils();
        Class<? extends RedisUtils> aClass1 = redisUtils.getClass();
        Field redisTemplate1 = aClass1.getDeclaredField("redisTemplate");
        redisTemplate1.setAccessible(true);
        redisTemplate1.set(redisUtils, redisTemplate);
        rabbitAdmin.declareExchange(ExchangeBuilder.directExchange(SecKillProperties.secKillDelayExchange).build());
        rabbitAdmin.declareQueue(SecKillDelayQueue.secKillDelayQueue());
        rabbitAdmin.declareBinding(SecKillDelayBinding.secKillDelayBinding());


    }


    //初始化库存
    @PostConstruct
    public void initGoodsNum(){
        RedisTemplate<String, Object> redisTemplate = RedisUtils.getRedisTemplate();
        Set<String> keys = redisTemplate.keys("*");
        if(!ObjectUtils.isEmpty(keys)){
            redisTemplate.delete(keys);
        }
        List<Goods> list = goodsService.list();
        ordersService.deleteAll();
        for (Goods goods : list) {
            goods.setStock(1000);
            goodsService.updateById(goods);
            redisTemplate.opsForValue().set(SecKillProperties.goods_nums_key +goods.getId(),goods.getStock());
        }
    }
}
